package servlet.customer;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Customer;
import service.CustomerService;

@WebServlet("/CustomerOperationServlet")
public class CustomerOperationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CustomerOperationServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CustomerService service = new CustomerService();
		
		String operation = request.getParameter("operation");
		
		String cid = request.getParameter("cid");
		String username = request.getParameter("username");
		String sex = request.getParameter("sex");
		String money = request.getParameter("money");
		String tel = request.getParameter("tel");
		String addr = request.getParameter("addr");
		String school = request.getParameter("school");
		String wechat = request.getParameter("wechat");
		
		String id = request.getParameter("id");
		
		switch(operation) {
		case "del":
			service.deleteById(id);
			break;
		case "alt": 
			if (id != null) {
				Customer customer = service.getCustomerById(id).list.get(0);
				request.setAttribute("operation", "alt");
				request.setAttribute("customer", customer);
				request.getRequestDispatcher("/admin/customerinfo.jsp").forward(request, response);
				return;
			}
			
			service.alterCustomer(cid, username, sex, money, tel, addr, school, wechat);
			response.sendRedirect("CustomerServlet?id=" + cid);
			break;
		case "add": 
//			if (name != null && !name.equals("")) {
//				service.addCustomer(cid, name, price, classify, stock, sales, goodDescribe, status);
//				response.sendRedirect("GoodServlet?id=" + sid);
//			}else {
//				request.setAttribute("operation", "add");
//				request.setAttribute("sid", sid);
//				request.getRequestDispatcher("/admin/goodinfo.jsp").forward(request, response);
//			}
			break;
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
